package com.spice.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.ChiliStudy;
import com.spice.service.mapper.ChiliStudyMapper;
import com.spice.service.service.ChiliStudyService;
import org.springframework.stereotype.Service;

@Service
public class ChiliStudyServiceImpl extends ServiceImpl<ChiliStudyMapper, ChiliStudy>
        implements ChiliStudyService {
    @Override
    public Response saveChiliStudy(ChiliStudy newChiliStudy) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(
                newChiliStudy.getChineseName(),
                newChiliStudy.getOrderId(),
                newChiliStudy.getMaintainer()
        )) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //检查序号是否重复
        if (this.count(new LambdaQueryWrapper<ChiliStudy>().eq(ChiliStudy::getOrderId, newChiliStudy.getOrderId())) > 0) {
            throw new SpiceException().message("序号与已有冲突");
        }
        //检查中文名称是否重复
        if (this.count(new LambdaQueryWrapper<ChiliStudy>().eq(ChiliStudy::getChineseName, newChiliStudy.getChineseName())) > 0) {
            throw new SpiceException().message("中文名称与已有冲突");
        }
        this.save(newChiliStudy);
        return Response.ok().message("辣椒科研信息添加成功").data("data", newChiliStudy);
    }

    @Override
    public Response removeChiliStudy(Long id) {
        if (CheckUtil.objectsHasNull(id)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认待删除数据是否存在
        ChiliStudy removedChiliStudy = this.getById(id);
        if (CheckUtil.objectsHasNull(removedChiliStudy)) {
            throw new SpiceException().message("该辣椒科研数据不存在");
        }
        this.removeById(id);
        return Response.ok().message("辣椒科研数据删除成功").data("data", removedChiliStudy);
    }

    @Override
    public Response updateChiliStudy(Long id, ChiliStudy newChiliStudy) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(id, newChiliStudy)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认更新的是否存在
        ChiliStudy updatedChiliStudy = this.getById(id);
        if (CheckUtil.objectsHasNull(updatedChiliStudy)) {
            throw new SpiceException().message("该辣椒科研数据不存在");
        }
        //确认更新后的序号是否与已有的冲突
        if (!newChiliStudy.getOrderId().equals(updatedChiliStudy.getOrderId())) {
            if (this.count(new LambdaQueryWrapper<ChiliStudy>().eq(ChiliStudy::getOrderId, newChiliStudy.getOrderId())) > 0) {
                throw new SpiceException().message("修改后的序号与已有序号冲突");
            }
        }
        //确认更新后的中文名称是否与已有的冲突
        if (!newChiliStudy.getChineseName().equals(updatedChiliStudy.getChineseName())) {
            if (this.count(new LambdaQueryWrapper<ChiliStudy>().eq(ChiliStudy::getChineseName, newChiliStudy.getChineseName())) > 0) {
                throw new SpiceException().message("修改后的中文名称与已有中文名称冲突");
            }
        }
        //更新待更新的数据的id和createTime
        newChiliStudy.setId(id);
        newChiliStudy.setCreateTime(updatedChiliStudy.getCreateTime());
        if (this.updateById(newChiliStudy)) {
            return Response.ok().message("辣椒科研数据修改成功").data("data", newChiliStudy);
        } else {
            return Response.error().message("辣椒科研数据修改失败");
        }
    }
}
